package edu.hrbu.kwaidu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hrbu.kwaidu.common.R;
import edu.hrbu.kwaidu.exception.CustomException;
import edu.hrbu.kwaidu.model.entity.SysRole;
import edu.hrbu.kwaidu.model.vo.SysRoleQueryVo;
import edu.hrbu.kwaidu.service.SysRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static edu.hrbu.kwaidu.constant.SystemConstant.EXCEPTION_PARAM_BLANK;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author xudengyu
 * @since 2023-12-29
 */
@RestController
@RequestMapping("/system/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @GetMapping("/{page}/{limit}")
    public R pageCondition(@PathVariable("page") Integer page,
                  @PathVariable("limit") Integer limit,
                  SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> pageParam=sysRoleService.pageCondition(page,limit,sysRoleQueryVo);
        return R.ok(pageParam);
    }

    @PostMapping("/getInfoById/{id}")
    public R getInfoById(@PathVariable("id") Long id){
        SysRole sysRole=sysRoleService.getInfoById(id);
        return R.ok(sysRole);
    }

    @PostMapping("/addOrEdit")
    public R addOrEdit(@RequestBody SysRole sysRole){
        Optional.ofNullable(sysRole).orElseThrow(()->new CustomException(EXCEPTION_PARAM_BLANK));
        sysRoleService.addOrEdit(sysRole);
        return R.ok();
    }

    @PostMapping("/del/{id}")
    public R del(@PathVariable("id") Long id){
        Optional.ofNullable(id).orElseThrow(()->new CustomException(EXCEPTION_PARAM_BLANK));
        sysRoleService.del(id);
        return R.ok();
    }

    @PostMapping("/delBatch")
    public R delBatch(@RequestBody ArrayList<Long> ids){
        if (ids.isEmpty()){
            throw new CustomException(EXCEPTION_PARAM_BLANK);
        }
        sysRoleService.delBatch(ids);
        return R.ok();
    }

    @GetMapping("/getList")
    public R getList(){
        List<SysRole> roleList=sysRoleService.getList();
        return R.ok(roleList);
    }

}
